package models;

import java.util.*;
import javax.persistence.*;
import play.db.jpa.*;
import play.db.jpa.Model;

@Entity
public class EEntity extends Model {

	public String name;
	@Lob
	String description;
	@ManyToOne
	Organization organization;
	@OneToMany(mappedBy = "entity", cascade = CascadeType.ALL)
	List<Topic> topics;
	@ManyToMany
	List<User> users;
	@ManyToMany
	List<User> organizers;

	public EEntity(String name, String description, Organization organization) {
		this.name = name;
		this.description = description;
		this.organization = organization;
		this.topics = new ArrayList<Topic>();
		this.users = new ArrayList<User>();
		this.organizers = new ArrayList<User>();
	}

	public List<EEntity> searchByKeyword(String keyword) {							//This method takes a keyword as input and returns a list of entities with names/descriptions matching that keyword

		List<EEntity> matchingTitle = (this.find("byName", keyword).fetch());		//put all entities with name matching keyword in matchingTitle list
		List<EEntity> matchingDescription = (this.find("byDescriptionLike", "%"		//put all entities with the keyword in the description in matchingDescription lis
				+ keyword + "%").fetch());

		List<EEntity> entitiesMatchingKeyword = new ArrayList<EEntity>(				//add the matchingTitle list to our final result
				matchingTitle);
		
		entitiesMatchingKeyword.addAll(matchingDescription);						//add the matchingDescription list to the final result
		return entitiesMatchingKeyword;												//return a list with all the matching entities

	}
}
